﻿#region License
/**	
The MIT License (MIT)

Copyright (c) 2013 Philipp Schillinger, Michael Alekseew, Andreas Braun, Steeven Zeiß

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
 **/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IndoorLocalization.UserInterface {
	/// <summary>
	/// Realizes a window that shows the current image of the cam.
	/// There is also the possibility of changing the displayed image to a motion detection image
	/// and switching the source is possible, too.
	/// </summary>
	partial class CamView : Form {

		/// <summary>
		/// The cam of which the image is displayed.
		/// </summary>
		private SensorCam cam;
		/// <summary>
		/// The mode of the shown image, e.g. normal image or motion detection.
		/// </summary>
		private DisplayMode mode = DisplayMode.RAW;

		/// <summary>
		/// The possible modes of displaying an image.
		/// </summary>
		public enum DisplayMode { RAW, MOTION, REFERENCE }

		/// <summary>
		/// Creates a new view window for the given camera.
		/// </summary>
		/// <param name="cam">The cam of which the image is displayed.</param>
		public CamView(SensorCam cam) {
			InitializeComponent();
			this.cam = cam;
		}
		/// <summary>
		/// Creates a new view window for the given camera in a certain display mode.
		/// </summary>
		/// <param name="cam">The cam of which the image is displayed.</param>
		/// <param name="mode">The desired display mode.</param>
		public CamView(SensorCam cam, DisplayMode mode) {
			InitializeComponent();
			this.cam = cam;
			this.mode = mode;
		}

		/// <summary>
		/// Loads the window by registrating some callbacks and setting its title.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void CamView_Load(object sender, EventArgs e) {
			// register callbacks
			cam.Updated += DisplayFrame;
			cam.Disposed += delegate {
				cam.Updated -= DisplayFrame;
				this.Close();
			};
			cam.NameChanged += ChangeTitle;

			// set window title
			Text = "View of "+cam.Name;
		}

		/// <summary>
		/// Declares this window as a clone of the given origin, which means they will have
		/// the same size and mode and are placed next to each other.
		/// </summary>
		/// <param name="origin">Window of which this is a clone.</param>
		private void CamView_Clone(CamView origin) {
			// copy size and mode
			Size = origin.Size;
			mode = origin.mode;

			// try to locate the window below origin
			if (origin.Location.Y + 2 * origin.Height <= Screen.GetWorkingArea(this).Height) {
				Location = new Point(
					origin.Location.X,
					origin.Location.Y + origin.Height);
			} else {
				Location = new Point(
					origin.Location.X + 20,
					origin.Location.Y + 20);
			}
		}

		/// <summary>
		/// Changes the mode to display the normal image.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void rawImageToolStripMenuItem_Click(object sender, EventArgs e) {
			mode = DisplayMode.RAW;
			rawImageToolStripMenuItem.Checked = true;
			motionDetectionToolStripMenuItem.Checked = false;
			referenceFrameToolStripMenuItem.Checked = false;
		}

		/// <summary>
		/// Changes the mode to display the motion detection image.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void motionDetectionToolStripMenuItem_Click(object sender, EventArgs e) {
			mode = DisplayMode.MOTION;
			rawImageToolStripMenuItem.Checked = false;
			motionDetectionToolStripMenuItem.Checked = true;
			referenceFrameToolStripMenuItem.Checked = false;
		}

		/// <summary>
		/// Changes the mode to display the reference frame.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void referenceFrameToolStripMenuItem_Click(object sender, EventArgs e) {
			mode = DisplayMode.REFERENCE;
			rawImageToolStripMenuItem.Checked = false;
			motionDetectionToolStripMenuItem.Checked = false;
			referenceFrameToolStripMenuItem.Checked = true;
		}

		/// <summary>
		/// Event that is triggered when the cam has a new image.
		/// Displays the new image in the window.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void DisplayFrame(object sender, EventArgs e) {
			try {
				switch (mode) {
					case DisplayMode.MOTION:
						viewBox.Image = cam.MotionFrame;
						break;
					case DisplayMode.REFERENCE:
						viewBox.Image = cam.RefFrameGray;
						break;
					default:
						viewBox.Image = cam.Frame;
						break;
				}
			} catch (InvalidOperationException) {
				cam.Updated -= DisplayFrame;
			}
		}

		/// <summary>
		/// Changes the name of this window.
		/// Is triggered when the name of the camera changes.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void ChangeTitle(object sender, EventArgs e) {
			Text = "View of " + cam.Name;
		}

		/// <summary>
		/// Event that is triggered when the window is closing.
		/// Removes all registered callbacks.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void CamView_FormClosing(object sender, FormClosingEventArgs e) {
			cam.Updated -= DisplayFrame;
			cam.NameChanged -= ChangeTitle;
		}

		/// <summary>
		/// Event that is triggered when the user changes the cam source of this window.
		/// </summary>
		/// <param name="sender">Button of the target cam, has the guid of the cam as tag.</param>
		/// <param name="e">Not used.</param>
		private void ChangeSource(object sender, EventArgs e) {
			cam.Updated -= DisplayFrame;	// remove callback at the old cam

			// finde new cam
			foreach (SensorCam newCam in Program.Cameras) {
				if (newCam.GUID == (Guid)((ToolStripButton)sender).Tag) {
					this.cam = newCam;
					break;
				}
			}

			Text = "View of " + cam.Name;	// update title
			cam.Updated += DisplayFrame;	// register the callback again at the new cam
		}

		/// <summary>
		/// Event that is triggered when the context menu opens, i.e. the user right-clicks the window.
		/// Initializes a list of all available cams for changing source.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
            if (mode == DisplayMode.RAW) {
                rawImageToolStripMenuItem.Checked = true;
				motionDetectionToolStripMenuItem.Checked = false;
				referenceFrameToolStripMenuItem.Checked = false;
			} else if (mode == DisplayMode.MOTION) {
                rawImageToolStripMenuItem.Checked = false;
				motionDetectionToolStripMenuItem.Checked = true;
				referenceFrameToolStripMenuItem.Checked = false;
			} else {
				rawImageToolStripMenuItem.Checked = false;
				motionDetectionToolStripMenuItem.Checked = false;
				referenceFrameToolStripMenuItem.Checked = true;
			}

			changeSourceToolStripMenuItem.DropDown.Items.Clear();	// remove old cams

			// initialize the list of all available cams
			foreach (SensorCam entryCam in Program.Cameras) {
				if (entryCam.GUID == cam.GUID) continue;
				ToolStripButton camButton = new ToolStripButton(entryCam.Name);
				camButton.Tag = entryCam.GUID;
				camButton.Click += ChangeSource;
				camButton.AutoSize = true;
				changeSourceToolStripMenuItem.DropDown.Items.Add(camButton);
			}

			// disable changing source if no source is available
			if (changeSourceToolStripMenuItem.DropDown.Items.Count == 0)
				changeSourceToolStripMenuItem.Enabled = false;
		}

		/// <summary>
		/// Event that is triggered when the user left-clicks the image box.
		/// Holding down shift while doing this clones this window.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void CamView_Click(object sender, EventArgs e) {
			// clone window if shift was pressed
			if (ModifierKeys == Keys.Shift) {
				CamView newView = new CamView(cam);
				newView.CamView_Clone(this);
				newView.Show();
			}
		}

	}
}
